package com.tensquare.manager.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;

@Component
public class ManagerFilter extends ZuulFilter {

    @Autowired
    private JwtUtil jwtUtil;

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        System.out.println("经过后台过滤器");
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();

        if(request.getMethod().equals("OPTIONS")){
            return null;
        }

        if(request.getRequestURI().indexOf("login")!=-1){
            return null;
        }
        String header = request.getHeader("Authorization");
        if(header!=null&&!"".equals(header)&&header.startsWith("Bearer ")){
            String token = header.substring(7);
            try {
                Claims claims = jwtUtil.parseJWT(token);
                String roles = (String) claims.get("roles");
                System.out.println("roles------------------------"+roles);
                if(roles!=null&&roles.equals("admin")){
                    context.addZuulRequestHeader("Authorization",header);
                    return null;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        context.setSendZuulResponse(false);//终止运行
        context.setResponseStatusCode(403);
        context.setResponseBody("权限不足");
        context.getResponse().setContentType("text/html;charset=utf-8");
        return null;
    }
}
